package com.cloud.shopping.listener;

import com.alibaba.fastjson.JSON;
import com.cloud.shopping.page.feign.SeckillPageFeign;
import com.cloud.shopping.search.feign.SeckillGoodsSearchFeign;
import com.cloud.shopping.search.model.SeckillGoodsEs;
import com.cloud.shopping.seckill.model.SeckillGoods;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

import javax.annotation.Resource;

/**
 * @author John.zhang
 * @version Id: SeckillGoodsHandler, v 1.0 2021/1/8 13:43 ZhiYong Exp $
 */

@CanalTable(value = "seckill_goods")
@Component
public class SeckillGoodsHandler implements EntryHandler<SeckillGoods> {

    @Resource
    private SeckillGoodsSearchFeign seckillGoodsSearchFeign;

    @Resource
    private SeckillPageFeign seckillPageFeign;

    /***
     * 增加商品
     * @param seckillGoods seckillGoods
     */
    @SneakyThrows
    @Override
    public void insert(SeckillGoods seckillGoods) {
        //索引导入
        seckillGoodsSearchFeign.add(JSON.parseObject(JSON.toJSONString(seckillGoods), SeckillGoodsEs.class));

        //生成/更新静态页
        seckillPageFeign.page(seckillGoods.getId());
    }


    /****
     * 修改商品详情
     * @param before before
     * @param after after
     */
    @SneakyThrows
    @Override
    public void update(SeckillGoods before, SeckillGoods after) {
        //索引导入
        seckillGoodsSearchFeign.add(JSON.parseObject(JSON.toJSONString(after), SeckillGoodsEs.class));

        //生成/更新静态页
        seckillPageFeign.page(after.getId());
    }
}
